﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;
using System.IO;

public partial class Administrator_AdminBannerIU : System.Web.UI.Page
{
    public Int32 bannerID = -1;
    public Int32 phanVungID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadPhanVungs();

            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";
        }

        try
        {
            bannerID = Int32.Parse(Request.QueryString["BannerID"].ToString());
        }
        catch (Exception ex)
        {
            bannerID = HangSo.DEFAULT_ID;
        }

        try
        {
            phanVungID = Int32.Parse(Request.QueryString["PhanVungID"].ToString());
        }
        catch (Exception ex)
        {
            phanVungID = HangSo.DEFAULT_ID;
        }

        if (bannerID == -1)
        {
            ViewState["action"] = HangSo.ACTION_ADD;
            this.imgBanner.ImageUrl = "~/ImageDatas/Banners/noImage.gif";
            this.ddlPhanVung.SelectedValue = phanVungID.ToString();
            this.lbAdminBanners.Text = "Thêm mới Banner";

        }
        else
        {
            Banner theBanner = BannerProvider.GetBannerByBannerID(bannerID);

            if (theBanner == null)
                Response.Redirect("AdminBanners.aspx");
            else
            {
                if (!Page.IsPostBack)
                {
                    this.lbAdminBanners.Text = "Sửa Banner";
                    this.txtNumberOfOrder.Text = theBanner.NumberOfOrder.ToString();
                    this.ddlPhanVung.SelectedValue = theBanner.PhanVungID.ToString();
                    this.imgBanner.ImageUrl = "~/ImageDatas/Banners/" + theBanner.BannerUrl;
                    this.fckGhiChuOther.Value = theBanner.GhiChuOther;
                    this.fckGhiChuVN.Value = theBanner.GhiChuVN;
                    this.ckPublish.Checked = theBanner.Publish;
                    this.txtUrl.Text = theBanner.Url;
                }

                ViewState["BannerID"] = theBanner.BannerID;
                ViewState["BannerUrl"] = theBanner.BannerUrl;
                ViewState["action"] = HangSo.ACTION_UPDATE;
            }
        }
    }

    /// <summary>
    /// Author : toanvt
    /// CreatedDate : 06/02/2012
    /// Description : Lấy danh sách Phân Vùng Banner
    /// </summary>
    private void LoadPhanVungs()
    {
        DataSet dsBanners = BannerProvider.GetPhanVungBanners();

        this.ddlPhanVung.DataTextField = "TitleOrName";
        this.ddlPhanVung.DataValueField = "PhanVungID";
        this.ddlPhanVung.DataSource = dsBanners;
        this.ddlPhanVung.DataBind();

        this.ddlPhanVung.Items.Insert(0, new ListItem("Chọn phân vùng", "-1"));
        this.ddlPhanVung.SelectedValue = phanVungID.ToString();
    }

    private Boolean ValidateData()
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        Boolean isSuccess = true;
        String message = "";

        if (ddlPhanVung.SelectedValue == "-1")
        {
            message += "Chọn phân vùng <br />";
            isSuccess = false;
        }


        if (!fuBannerUrl.HasFile && ViewState["action"].ToString() == HangSo.ACTION_ADD)
        {
            message += "Chưa chọn hình ảnh ";
            isSuccess = false;
        }

        if (!isSuccess)
        {
            this.lbMessage.Text = message;
        }

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 bannerIDOut = -1;
            Banner theBanner = new Banner();
            theBanner.GhiChuOther = this.fckGhiChuOther.Value;
            theBanner.GhiChuVN = this.fckGhiChuVN.Value;
            theBanner.Publish = this.ckPublish.Checked;
            theBanner.PhanVungID = Int32.Parse(this.ddlPhanVung.SelectedValue);
            theBanner.Url = this.txtUrl.Text.Trim();

            try
            {
                theBanner.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theBanner.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            String action = ViewState["action"].ToString();

            if (this.fuBannerUrl.HasFile)
            {
                String fileName = fuBannerUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuBannerUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Banners//" + newFileName);
                theBanner.BannerUrl = newFileName.Trim();
            }
            else
            {
                if (action == HangSo.ACTION_UPDATE)
                {
                    theBanner.BannerUrl = ViewState["BannerUrl"].ToString();
                }
                else
                {
                    theBanner.BannerUrl = "noImage.gif";
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theBanner.BannerID = -1;
                bannerIDOut = BannerProvider.IUBanner(theBanner, true);
            }
            else
            {
                theBanner.BannerID = Int32.Parse(ViewState["BannerID"].ToString());
                bannerIDOut = BannerProvider.IUBanner(theBanner, false);
            }

            Response.Redirect("AdminBanners.aspx?PhanVungID=" + phanVungID.ToString());
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminBanners.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 bannerIDOut = -1;
            Banner theBanner = new Banner();
            theBanner.GhiChuOther = this.fckGhiChuOther.Value;
            theBanner.GhiChuVN = this.fckGhiChuVN.Value;
            theBanner.Publish = this.ckPublish.Checked;
            theBanner.PhanVungID = Int32.Parse(this.ddlPhanVung.SelectedValue);
            theBanner.Url = this.txtUrl.Text.Trim();

            try
            {
                theBanner.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theBanner.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            String action = ViewState["action"].ToString();

            if (this.fuBannerUrl.HasFile)
            {
                String fileName = fuBannerUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuBannerUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Banners//" + newFileName);
                theBanner.BannerUrl = newFileName.Trim();
            }
            else
            {
                if (action == HangSo.ACTION_UPDATE)
                {
                    theBanner.BannerUrl = ViewState["BannerUrl"].ToString();
                }
                else
                {
                    theBanner.BannerUrl = "noImage.gif";
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theBanner.BannerID = -1;
                bannerIDOut = BannerProvider.IUBanner(theBanner, true);
            }
            else
            {
                theBanner.BannerID = Int32.Parse(ViewState["BannerID"].ToString());
                bannerIDOut = BannerProvider.IUBanner(theBanner, false);
            }

            Response.Redirect("AdminBannerIU.aspx?BannerID=" + bannerIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
